<template>
  <div>
    <TTbale
      :listData="dataList"
      v-bind="contentTableConfig"
      :listCount="dataCount"
      v-model:page="pageInfo"
    >
      <template #handlerHeadler>
        <el-button
          v-if="isCreate"
          size="mini"
          type="info"
          icon="el-icon-s-custom"
          @click="newUser"
          >新建用户</el-button
        >
        <el-button size="mini" icon="el-icon-orange" type="warning"></el-button>
      </template>
      <template #status="scope">
        <el-button size="mini" :type="scope.row.enable ? 'primary' : 'info'">{{
          scope.row.enable ? '可用' : '禁 用'
        }}</el-button>
      </template>
      <template #createAt="scope">
        <strong> {{ $filters.formatTime(scope.row.createAt) }}</strong>
      </template>
      <template #updateAt="scope">
        <strong> {{ $filters.formatTime(scope.row.updateAt) }}</strong>
      </template>
      <template #CRUD="scope">
        <div class="CRUD-btns">
          <el-button
            type="text"
            icon="el-icon-edit"
            size="mini"
            v-if="isUpdate"
            @click="editClick(scope.row)"
          >
            编辑</el-button
          >
          <el-button
            type="danger"
            icon="el-icon-delete-solid"
            size="mini"
            v-if="isDelete"
            @click="deleteClick(scope.row)"
          >
            删除</el-button
          >
        </div>
      </template>
      <template
        v-for="item in otherPropsSlots"
        :key="item.prop"
        #[item.slotName]="scope"
      >
        <template v-if="item.slotName">
          <slot :name="item.slotName" :row="scope.row"> </slot>
        </template>
      </template>
    </TTbale>
  </div>
</template>

<script lang="ts" setup>
import {
  defineProps,
  defineExpose,
  computed,
  ref,
  watch,
  defineEmits
} from 'vue'
import { useStore } from 'vuex'
import TTbale from '@/base-ui/table'
import { usePermission } from '@/hooks/use-permissions'

const emit = defineEmits(['newBtn', 'editBtn'])

const props = defineProps({
  contentTableConfig: {
    type: Object,
    required: true
  },
  pageName: {
    type: String,
    required: true
  }
})

const isCreate = usePermission(props.pageName, 'create')
const isUpdate = usePermission(props.pageName, 'update')
const isDelete = usePermission(props.pageName, 'delete')
const isQuery = usePermission(props.pageName, 'query')

const pageInfo = ref({ currentPage: 1, pageSize: 10 })
watch(pageInfo, () => getPageData())

const dataList = computed(() =>
  store.getters[`system/pageListData`](props.pageName)
)
const dataCount = computed(() =>
  store.getters[`system/pageListCount`](props.pageName)
)

const store = useStore()
const getPageData = (queryInfo: any = {}) => {
  if (!isQuery) return
  store.dispatch('system/getPageListAction', {
    pageName: props.pageName,
    queryInfo: {
      offset: (pageInfo.value.currentPage - 1) * pageInfo.value.pageSize,
      size: pageInfo.value.pageSize,
      ...queryInfo
    }
  })
}
getPageData()

defineExpose({
  getPageData
})

const otherPropsSlots = props.contentTableConfig?.propList.filter(
  (item: any) => {
    if (item.slotName === 'status') return false
    if (item.slotName === 'createAt') return false
    if (item.slotName === 'updateAt') return false
    if (item.slotName === 'handlerHeadler') return false
    if (item.slotName === 'CRUD') return false
    return true
  }
)

const deleteClick = (item: any) => {
  store.dispatch('system/deletePageDataAction', {
    pageName: props.pageName,
    id: item.id
  })
}

const newUser = () => {
  emit('newBtn')
}
const editClick = (item: any) => {
  emit('editBtn', item)
}
</script>
<style lang="less" scoped></style>
